/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

 //公众号的id
var accountid;
var qyhid;
//企业应用id
var agentid;

var menuid;
 //获取窗口的高度 
var windowHeight; 
//获取窗口的宽度 
var windowWidth; 
//获取弹窗的宽度 
var popWidth; 
//获取弹窗高度 
var popHeight;
//获取数据路径
var urlgetdata;
//删除子菜单路径
var urldelsecondmenu;
//修改菜单路径
var urlupdatename;
//添加一级子菜单路径;
var urlcheckfir;
//动作按钮路径
var urlgetactive;
//动作按钮保存路径
var urlupdateactive;
//保存url
var urlsave;

$(function(){
    //点击菜单按钮弹出,获取数据
    $("button[title='菜单']").each(function(){
       $(this).on('click',function(){
           accountid=$(this).attr('name');
           $("#accountid").val(accountid);
           agentid = $(this).attr('agentid');
           qyhid = $(this).attr('qyhid');
           if((location.href).indexOf("r=gky-we-account")>=0){
               urlgetdata='index.php?r=gky-we-account/menu';
               urldelsecondmenu='index.php?r=gky-we-account/delsecond';
               urlupdatename='index.php?r=gky-we-account/updatename';
               urlcheckfir='index.php?r=gky-we-account/checkfir';
               urlgetactive='index.php?r=gky-we-account/getactive';
               urlupdateactive='index.php?r=gky-we-account/updateactive';
               urlsave = 'index.php?r=gky-we-menu/set-menu';
           }else if((location.href).indexOf("r=gky-qyh")>=0){
               urlgetdata='index.php?r=gky-qyh/menu';
               urldelsecondmenu='index.php?r=gky-qyh/delsecond';
               urlupdatename='index.php?r=gky-qyh/updatename';
               urlcheckfir='index.php?r=gky-qyh/checkfir';
               urlgetactive='index.php?r=gky-qyh/getactive';
               urlupdateactive='index.php?r=gky-qyh/updateactive';
               urlsave = 'index.php?r=gky-qyh-menu/set-menu';
           }
           getdata();
       });
   });
   
    //保存提示按钮
    $('#btnsave').click(function(){
        $.ajax({
            url:urlsave,
            type:'post',
            data:{
                'accountid':accountid,
                'qyhid':qyhid,
                'agentid':agentid
            },
            dataType:'json',
            success:function(data){
               if(data.status == 0){
                   alert('保存成功')
                   $("#modal").modal('hide');
               }else{
                   alert('保存失败')
               }
            }
        });

    });
    
     $('#addfir').click(function(){
        //检测包含有3个一级菜单
        $.ajax({
            url:urlcheckfir,
            type:'post',
            data:{'accountid':accountid},
            dataType:'json',
            success:function(response){
                if(response.res){
                    var str='<div class="col-lg-12 col-lg-push-1">'
                    str+='<input type="text" onchange="firstchange(this)" name="insertfir" class="form-control" style="float: left; width: 220px" />';
                    str+='<input type="button" class="btn btn-default" style="float: left; margin-left: 10px" value="动作" />';
                    str+='<input type="button" onclick="delsecondmenu(this,\'sec\')" class="btn btn-default" style="float: left; margin-left: 10px" value="删除" /></div>';
                    $('#addfirst').append(str); 
                }else{
                    alert("已经含有3个一级菜单，不可再添加了");
                }
            }
        });
    });
    
    //动作按钮关键字和链接的切换
    $('input[name=active]').click(function(){
        if($(this).attr('value')=='key'){
            $('input[name="txtkey"]').show();
            $('input[name="txturl"]').hide();
        }else if($(this).attr('value')=='url'){
            $('input[name="txtkey"]').hide();
            $('input[name="txturl"]').show();
        }
    });
    
    //动作按钮的保存
     $("#activesave").click(function(){
      var active=$('input:radio[name="active"]:checked').val();
      var txturl=$('input[name="txturl"]').val();
      var txtkey=$('input[name="txtkey"]').val();
      $.ajax({
         url:urlupdateactive,
         type:'post',
         data:{'active':active,'menuid':menuid,'txturl':txturl,'txtkey':txtkey},
         dataType:'json',
         success:function(response){
             if(response.res){
                 alert("保存成功！");
                 $('.window').hide('slow');
             }else{
                 alert("保存失败！");
                 $('.window').hide('slow');
             }
         }
      });
   });
    
});

/*
 * 动作按钮的弹出框
 */
    function init(){ 
       windowHeight=$(window).height(); 
       windowWidth=$("#firstmenu").width(); 
       popHeight=$(".window").height(); 
       popWidth=$(".window").width(); 
    } 
    //关闭窗口的方法 
    function closeWindow(){ 
     $(".title span").click(function(){ 
         $(this).parent().parent().hide("slow"); 
         }); 
     } 
     //定义弹出居中窗口的方法 
     function popCenterWindow(){ 
        init(); 
         //计算弹出窗口的左上角Y的偏移量 
        var popY=(windowHeight-2*popHeight)/2; 
        var popX=(windowWidth-popWidth)/2; 
        //设定窗口的位置 
        $("#center").css("top",popY).css("left",popX).slideToggle("slow");  
        closeWindow();
     }
     
//获取数据
function getdata(){
 $.ajax({
     url:urlgetdata,
     type:"post",
     data:{'accountid':accountid},
     dataType:"json",
     success:function(response){
         $('#firstmenu').html("");
         $('#addfirst').html("");
         var menu=response.gkywemenu;
         if(menu.length>0){
             for(var i=0;i<menu.length;i++){
                 //如果pid为0，则为一级菜单
                 if(menu[i]['pid']==0){
                     var str='<div class="col-lg-12 col-lg-push-1">';
                     str+='<input type="text" class="form-control" onchange="firstchange(this)" name='+menu[i]['id']+' value='+menu[i]['name']+' style="float: left; width: 220px" />';
                     str+='<input type="button" onclick="btnactive(this)" name='+menu[i]['id']+' class="btn btn-default" style="float: left; margin-left: 10px" value="动作" />';
                     str+=' <input type="button" class="btn btn-default" name="'+menu[i]['id']+'" onclick="delsecondmenu(this,\'fir\')" style="float: left; margin-left: 10px" value="删除" />';
                     str+='<input type="button" onclick="addsecondmenu(this,'+i+')" name='+menu[i]['id']+' class="btn btn-default" style="float: left; margin-left: 10px" value="添加子菜单" /></div>';
                     $('#firstmenu').append(str);
                     //遍历出对应的二级子菜单
                     for(var j=0;j<menu.length;j++){
                         if(menu[j]['pid']==menu[i]['id']){
                             var str2='<div class="col-lg-12 col-lg-push-2">'
                             str2+='<input type="text" class="form-control" onchange="firstchange(this)" name='+menu[j]['id']+' value='+menu[j]['name']+' style="float: left; width: 220px" />';
                             str2+='<input type="button" onclick="btnactive(this)" name='+menu[j]['id']+' class="btn btn-default" style="float: left; margin-left: 10px" value="动作" />';
                             str2+='<input type="button" name="'+menu[j]['id']+'" onclick="delsecondmenu(this,\'sec\')" class="btn btn-default" style="float: left; margin-left: 10px" value="删除" /></div>';
                             $('#firstmenu').append(str2);
                         }
                     }
                     //添加一个div方便后面动态添加子菜单
                     var str3='<div class="'+i+'"></div>';
                     $('#firstmenu').append(str3);
                 }
            }
         }
     }
      }); 
    }
    
       //添加子菜单
function addsecondmenu(obj,i){
    var str='<div class="col-lg-12 col-lg-push-2">'
    str+='<input type="text" onchange="firstchange(this)" name="insert'+$(obj).attr('name')+'" class="form-control" style="float: left; width: 220px" />';
    str+='<input type="button" class="btn btn-default" style="float: left; margin-left: 10px" value="动作" />';
    str+='<input type="button" onclick="delsecondmenu(this,\'sec\')" class="btn btn-default" style="float: left; margin-left: 10px" value="删除" /></div>';
    $('.'+i).append(str); 
}

//删除子菜单,str判断是一级还是子菜单
    function delsecondmenu(obj,str){
        var secondid=$(obj).attr('name');
        var check=confirm("你确定要删除吗？");
        if(check){
             if(!secondid){
                getdata();
            }else{
                $.ajax({
                    url:urldelsecondmenu,
                    type:'post',
                    data:{'secondid':secondid,'str':str},
                    dataType:'json',
                    success:function(response){
                    if(response.res){
                        alert("删除成功！");
                        getdata();
                    }else{
                        alert("删除失败！");
                    }
                }
             });
            }
        }
    }
    
//修改菜单
function firstchange(obj){
    var fandsid=$(obj).attr('name');
    var name=$(obj).val();
    $.ajax({
       url:urlupdatename,
       type:'post',
       data:{'accountid':accountid,'fandsid':fandsid,'name':name},
       dataType:'json',
       success:function(response){
           if(response.res){
               getdata();
               $('#addfirst').html("");
           }else{
               alert("修改失败!");
               getdata();
           }
       }
    }); 
}

//动作按钮点击显示信息
function btnactive(obj){
    popCenterWindow();
    menuid=$(obj).attr('name');
    $.ajax({
       url:urlgetactive,
       type:'post',
       data:{'menuid':menuid},
       dataType:'json',
       success:function(response){
           $('input[name="txturl"]').val(response.url);
           $('input[name="txtkey"]').val(response.key);
       }
    });
}

 



